package com.tarcemap.gpsmobileappgui;

import java.util.Iterator;
import java.util.List;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.tracemap.gpslocation.R;

public class OverLayRoute extends Overlay  
{  
	Resources resource;
	List<GeoPoint> mLocations;  
    //在更新坐标时，设置该坐标，以便画图  
	
	public OverLayRoute(Resources resource)
	{
	}
   
    public void setLocation(List<GeoPoint> locations)  
    {  
    	mLocations = locations;  
    }  
    @Override 
    public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when)  
    {  
    	int i=0;
    	if (mLocations.size()==0)
    		return false;
        super.draw(canvas, mapView, shadow);  
        Paint paint = new Paint();  
        Point myScreenCoords = new Point();  
        // 将经纬度转换成实际屏幕坐标  
        GeoPoint tmpGeoPoint;
        float[] pointsLine=new float[mLocations.size()*4];
        float tmpPreviousLat = 0;
        float tmpPreviousLng = 0;
        for (Iterator<GeoPoint> iterator = mLocations.iterator(); iterator.hasNext();) {
        	
        	GeoPoint loc = (GeoPoint) iterator.next();
			tmpGeoPoint = new GeoPoint((int)(loc.getLatitudeE6()),(int)(loc.getLongitudeE6()));  
	        
	        mapView.getProjection().toPixels(tmpGeoPoint,myScreenCoords);  
	        paint.setStrokeWidth(1);  
	        paint.setARGB(255, 255, 0, 0);  
	        paint.setStyle(Paint.Style.STROKE);  
	        Bitmap bmp = BitmapFactory.decodeResource(resource, R.drawable.pushpin);  
	        canvas.drawBitmap(bmp,myScreenCoords.x,myScreenCoords.y,paint);  
	        canvas.drawText("Num:"+String.valueOf(i),myScreenCoords.x,myScreenCoords.y, paint);
	        
	        if (i>0)
	        {
	        	pointsLine[i] = tmpPreviousLat;
	        	pointsLine[i+1] = tmpPreviousLng ;
	        	pointsLine[i+2] = (float) (loc.getLatitudeE6()/1E6);
	        	pointsLine[i+3] = (float) (loc.getLongitudeE6()/1E6);
	        }
	        tmpPreviousLat = pointsLine[i+2];
	        tmpPreviousLng = pointsLine[i+3];
	        i = i + 4;
		}
        
        canvas.drawLines(pointsLine, paint);
        return true;  
    }
}
    
